package com.quick.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.common.Constent.Quick;
import com.quick.common.Exception.exception.BaseException;
import com.quick.common.bean.User;
import com.quick.common.dto.*;
import com.quick.common.service.IUserService;
import com.quick.common.util.PasswordUtil;
import com.quick.common.util.TokenUtil;
import com.quick.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public String userLogin(LoginDTO loginDTO) {
        if (loginDTO.getName() == null || loginDTO.getName().isEmpty()){
            throw new BaseException(400,"用户名不能为空");
        }
        if (loginDTO.getPassword() == null || loginDTO.getPassword().isEmpty()){
            throw new BaseException(400,"密码不能为空");
        }
        String name = loginDTO.getName();
        User user = super.getOne(new QueryWrapper<User>().eq("name", name));
        if (user == null){
            throw new BaseException(400,"用户不存在");
        }
        // 增加对密码字段的校验
        if (user.getPassword() == null || user.getPassword().isEmpty()) {
            throw new BaseException(500,"用户密码数据异常");
        }
        // 使用try-catch包装密码校验，防止异常泄露
        try {
            if (!PasswordUtil.checkPassword(loginDTO.getPassword(), user.getPassword())){
                throw new BaseException(400,"密码错误");
            }
        } catch (Exception e) {
            throw new BaseException(400,"密码错误");
        }
        System.out.println("id"+user.getId());
        return TokenUtil.getToken(user.getId(), user.getName(), Quick.TOKEN_KEY_ROLE_USER);
    }

    @Override
    public void userRegister(RegisterDTO userRegisterDTO) {
        if (userRegisterDTO.getName() == null || userRegisterDTO.getName().isEmpty()){
            throw new BaseException(400,"用户名不能为空");
        }
        if (userRegisterDTO.getPassword() == null || userRegisterDTO.getPassword().isEmpty()){
            throw new BaseException(400,"密码不能为空");
        }
        if (userRegisterDTO.getRePassword() == null || userRegisterDTO.getRePassword().isEmpty()){
            throw new BaseException(400,"确认密码不能为空");
        }
        if (!userRegisterDTO.getPassword().equals(userRegisterDTO.getRePassword())){
            throw new BaseException(400,"密码不一致");
        }
        if(super.getOne(new QueryWrapper<User>().eq("name", userRegisterDTO.getName()))!= null){
            throw new BaseException(400,"用户已存在");
        }
        User user = new User();
        BeanUtils.copyProperties(userRegisterDTO, user);
        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        super.save(user);
    }

    @Override
    public User getUserById(Integer id) {
        return super.getById(id);
    }

    @Override
    public int addUser(User user) {
        return super.save(user) ? 1 : 0;
    }

    @Override
    public int editUser(User user) {
        return super.updateById(user) ? 1 : 0;
    }

    @Override
    public int deleteUser(Integer id) {
        return super.removeById(id) ? 1 : 0;
    }

    @Override
    public int reverseStatus(Integer id) {
        User user = super.getById(id);
        if (user != null) {
            user.setStatus(user.getStatus().equals(1) ? 0 : 1);
        }if (super.updateById(user)) {
            return 1;
        }
        else return 0;
    }

    @Override
    public User getUserByPhone(String phone) {
        return super.getOne(new QueryWrapper<User>().eq("phone", phone));
    }

    @Override
    public User getUserByName(String name) {
        return super.getOne(new QueryWrapper<User>().eq("name", name));
    }

    @Override
    public User getUserLoginBeanById(Integer id) {
        User user = super.getById(id);
        return user;
    }

    @Override
    public String upload(ImageDTO imageDTO) {
        User user=new User();
        BeanUtils.copyProperties(imageDTO, user);
        super.updateById(user);
        User user1 = super.getById(user.getId());
        return user1.getImg();
    }

    @Override
    public void changePassword(PasswordDTO passwordDTO) {
        User user = super.getById(passwordDTO.getId());
        if (user == null){
            throw new BaseException(400,"用户不存在");
        }
        if (!PasswordUtil.checkPassword(passwordDTO.getOldPassword(), user.getPassword())){
            throw new BaseException(400,"密码错误");
        }
//        if(!PasswordUtil.checkPassword(passwordDTO.getNewPassword(),user.getPassword())){
//            throw new BaseException(400,"新密码不能与旧密码一致");
//        }
        user.setPassword(PasswordUtil.getEncryption(passwordDTO.getNewPassword()));
        super.updateById(user);
    }
}
